#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//字符串暴力匹配
int index(char* baseString, char* subString);

int main()
{
    char* baseString = "ababcabcacbab";
    char* subString = "abcac";
    printf("%d\n", index(baseString, subString));

    return 0;
}

int index(char* baseString, char* subString)
{
    int i = 1, j = 1;
    while(i <= strlen(baseString) && j <= strlen(subString))
    {
        if(baseString[i] == subString[j])         //当前字符匹配相同，继续向后匹配
        {
            ++i;
            ++j;
        } else {
            i = i-j+2;                            //如果匹配失败，i回退到原来的“i+1”位置处,j指针回退到开头继续遍历
            j = 1;
        }
    }
    if(j < strlen(subString)) return i - strlen(subString); //如果子串遍历j大于子串的长度，说明遍历成功，
                                                        //当前主串i的位置减去子串的长度就是子串相同于主串的开始下标
    return 0;
}